package com.tansun.easycare.rule.common.jfun;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import com.alibaba.fastjson.JSONArray;
import com.tansun.easycare.rule.common.service.SpringInstanceUtils;
import com.tansun.rule.utils.JavaUnmarshaller;

/**
 * 函数管理
 *
 * @author WY
 */
@XmlRootElement(name = "FunConfig")
public class JFunCreator {

    private static List<JFun> jFunList = new ArrayList<JFun>();


    @XmlElementWrapper(name = "funs")
    @XmlElement(name = "fun")
    public List<JFun> getjFunList() {
        return jFunList;
    }

    /**
     * set
     */
    public void setjFunList(List<JFun> jFunList) {
        JFunCreator.jFunList = jFunList;
    }

    /**
     * 初始化系统函数列表
     */
    public static String initJFun(List<JFun> list) throws Exception {
        String path = SpringInstanceUtils.instance.getResource("classpath:config/jfun.xml").getURL().getPath();
        JFunCreator jfc = (JFunCreator) JavaUnmarshaller.xml2Bom(path , JFunCreator.class);
        jFunList = jfc.getjFunList();

        String userFunPath = SpringInstanceUtils.instance.getResource("classpath:config/userjfun.xml").getURL().getPath();
        List<JFun> userFuns = UserJFunCreator.getUserFuns(userFunPath);
        userFuns.addAll(jFunList);
        if(list!=null)userFuns.addAll(list);
        return JSONArray.toJSONString(userFuns);
    }

    /**
     * 获取函数 by id
     */
    public static JFun getJFunById(String id, String path) throws Exception {

        List<JFun> userFuns = UserJFunCreator.getUserFuns(path);
        userFuns.addAll(jFunList);
        for (JFun jFun : userFuns) {
            if (jFun.getId().equals(id)) {
                return jFun;
            }
        }
        return null;
    }
    private static List<JFun> allFunList = new ArrayList<JFun>();
    
    public static JFun getJFunById(String id) throws Exception {
    	if(allFunList.size()==0) {
        	String[] pathList = new String[] {"config/jfun.xml","config/userjfun.xml"};
    		for (String path : pathList) {
    			String realPath = JFunCreator.class.getClassLoader().getResource(path).getPath();
    			List<JFun> userFuns = UserJFunCreator.getUserFuns(realPath);
    			allFunList.addAll(userFuns);
			}
    	}
        for (JFun jFun : allFunList) {
            if (jFun.getId().equals(id)) {
                return jFun;
            }
        }
        return null;
    }
    //	public static Map<String, JFun> getJFunMap() throws Exception{
//		
//		return null;
//	}
    public static void main(String[] args) throws Exception {
        JFunParam pdate1 = new JFunParam();
        pdate1.setDataType("Date");
        pdate1.setName("date1");
        pdate1.setSort(0);

        JFunParam pdate2 = new JFunParam();
        pdate2.setDataType("Date");
        pdate2.setName("date2");
        pdate2.setSort(1);

        pdate2.getNameValues().add(new NameValue("name1", "value1"));
        pdate2.getNameValues().add(new NameValue("name2", "value2"));


        JFun jfun = new JFun();
        jfun.setId("qwerfdsa");
        jfun.setName("日期差之天数");
        jfun.setLabel("日期差之天数");
        jfun.setDataType("Long");
        jfun.setType("fn");
        jfun.setFunType("ftdate");
        jfun.setShowFormat("{date1}比{date2}多的天数");
        jfun.setDesc("日期差");
        jfun.getParamMap().put(pdate1.getName(), pdate1);
        jfun.getParamMap().put(pdate2.getName(), pdate2);

        JFunCreator jfc = new JFunCreator();
        jfc.getjFunList().add(jfun);
        String path = JFunCreator.class.getResource("/config").getPath();
        System.out.println(path);
        JavaUnmarshaller.bom2Xml(path + "/jfun.xml", jfc, JFunCreator.class);


    }

    public static void main1(String[] args) throws Exception {
        //System.out.println(JFunCreator.initJFun());
//		
//		String path = JFunCreator.class.getResource("/config").getPath();
//		JFunCreator jfc = (JFunCreator)JavaUnmarshaller.xml2Bom(path+"/jfun.xml", JFunCreator.class);
//		System.out.println(JSONArray.toJSONString(jfc.getjFunList()));
//		
//		System.out.println(jfc.getjFunList().size());
    }
}
